package RDFManager;

import org.apache.jena.rdf.model.Model;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * {@code RDFOutput}类提供使用RDF model生成RDF文件的方法
 */
public class RDFOutput {
  /**
   * 以Turtle形式生成RDF文件
   *
   * @param outputFileName 输出文件名称
   */
  public static void out_turtle(Model model, String outputFileName) {
    FileOutputStream fos;
    try {
      fos = new FileOutputStream(outputFileName);
    } catch (FileNotFoundException e) {
      throw new RuntimeException(e);
    }

    model.write(fos, "TURTLE");
    try {
      fos.close();
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
    System.out.println("Model saved to " + outputFileName);
  }


  /**
   * 以N-Triples格式生成RDF文件
   *
   * @param outputFileName 输出文件名称
   */
  public static void out_nTriples(Model model, String outputFileName) {
    FileOutputStream fos;
    try {
      fos = new FileOutputStream(outputFileName);
    } catch (FileNotFoundException e) {
      throw new RuntimeException(e);
    }

    model.write(fos, "N-TRIPLES");
    try {
      fos.close();
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
    System.out.println("Model saved to " + outputFileName);
  }


  /**
   * 以RDF/XML格式生成RDF文件
   *
   * @param outputFileName 输出文件名称
   */
  public static void out_rdf(Model model, String outputFileName) {
    FileOutputStream fos;
    try {
      fos = new FileOutputStream(outputFileName);
    } catch (FileNotFoundException e) {
      throw new RuntimeException(e);
    }

    model.write(fos, "RDF/XML-ABBREV");
    try {
      fos.close();
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
    System.out.println("Model saved to " + outputFileName);
  }
}
